package com.bacon.gmall.list.controller;

import com.bacon.gmall.common.result.Result;
import com.bacon.gmall.list.service.ListService;
import com.bacon.gmall.model.list.Goods;
import com.bacon.gmall.model.list.SearchParam;
import com.bacon.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2020-12-28 18:28
 * @description:
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {


    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private ListService listService;

    //开始搜索
    @PostMapping("/")
    public SearchResponseVo list(@RequestBody SearchParam searchParam) {
        return listService.list(searchParam);
    }

    @GetMapping("/mappings")
    public Result mappingsES() {

        //创建库
        elasticsearchRestTemplate.createIndex(Goods.class);

        //映射 表 字段 字段类型
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        listService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId) {
        listService.cancelSale(skuId);
        return Result.ok();
    }

    //更新热度
    @GetMapping("/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId) {
        listService.hotScore(skuId);
        return Result.ok();
    }
}
